<?php
namespace catchAdmin\apipatient\controller;

use think\Request;
use catcher\CatchResponse;
use app\common\utils\Upfile;
use catcher\base\CatchController;
use app\common\model\PatientRecord as PatientRecordModel;
use catchAdmin\apipatient\validate\HealthyValidate;
use think\exception\ValidateException;

class Healthy extends CatchController
{ 
    /**
     * 我的病历
     *
     * @param Request $request
     * @return \think\response\Json
     */
    public function get_my_case(Request $request): \think\response\Json
    {
        return CatchResponse::success((new PatientRecordModel)->getWherePatientId(request()->userInfo['patientId']));
    }

    /**
     * 病历详情
     *
     * @param Request $request
     * @return \think\response\Json
     */
    public function get_my_case_detail(Request $request): \think\response\Json
    {
        $req = $request->param();

        #验证参数
        try {
            validate(HealthyValidate::class)->scene('get_my_case_detail')->check($req);
            return CatchResponse::success((new PatientRecordModel)->getRecordDetail($req['id']));
        } catch (ValidateException $e) {
            return CatchResponse::fail($e->getError());
        }
    } 

    /**
     * 病历统计
     *
     * @param Request $request
     * @return \think\response\Json
     */ 
    public function get_my_case_report(Request $request): \think\response\Json
    {
        return CatchResponse::success((new PatientRecordModel)->getRecordStatistics(request()->userInfo['patientId']));

    }

    /**
     * 病历时间轴
     *
     * @param Request $request
     * @return \think\response\Json
     */ 
    public function get_time_case_report(Request $request): \think\response\Json
    {
        $req = $request->param();

        #验证参数
        try {
            validate(HealthyValidate::class)->scene('get_time_case_report')->check($req);
            if ($req['type'] == 3)
            {
                return CatchResponse::success((new PatientRecordModel)->getPatientIdDatas(request()->userInfo['patientId'], $req['type']));
            }
            return CatchResponse::paginate((new PatientRecordModel)->getPatientIdDatas(request()->userInfo['patientId'], $req['type']));
        } catch (ValidateException $e) {
            return CatchResponse::fail($e->getError());
        }
    }

    /**
     * 病历详情上传多图
     *
     * @param Request $request
     * @return \think\response\Json
     */    
    public function uploadimg(Request $request): \think\response\Json
    {
        $req = $request->param();

        #验证参数
        try {
            validate(HealthyValidate::class)->scene('uploadimg')->check($req);
            $record = (new PatientRecordModel)->getWhereId($req['id'], 'images');

            if (isset($record['images']) && !empty($record['images']))
            {
                if (count($record['images']) >= 5 || count($request->file()) > 5)
                {
                    return CatchResponse::fail('不可超过5张图片');
                }
    
                $image = (new Upfile)->upLoad($request->file(), 1);
                $data['images'] = $record['images'];
                $data['images'][] = $image;
                sort($data['images']);
            }else{
                $image = (new Upfile)->upLoad($request->file(), 1);
                $data['images'][] = $image;
            }
            $result = (new PatientRecordModel)->saveData($req['id'], $data);
            return CatchResponse::success($result);
        } catch (ValidateException $e) {
            return CatchResponse::fail($e->getError());
        }
    }

    /**
     * 删除病历图片
     *
     * @param Request $request
     * @return \think\response\Json
     */ 
    public function delimg(Request $request): \think\response\Json
    {
        $req = $request->param();

        #验证参数
        try {
            validate(HealthyValidate::class)->scene('delimg')->check($req);
            $result = (new PatientRecordModel)->delImage($req['id'], $req['key']);
            if ($result)
            {
                return CatchResponse::success($result);
            }
            return CatchResponse::fail('删除失败');
        } catch (ValidateException $e) {
            return CatchResponse::fail($e->getError());
        }
    }
}